<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item v-if="this.Idtype == 0 || this.Idtype == 1" label="商家名称" prop="storeName">
        <el-input v-model="queryParams.storeName" placeholder="请输入商家名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item v-if="this.Idtype == 0 || this.Idtype == 1" label="代理商名称" prop="agentName">
        <el-input v-model="queryParams.agentName" placeholder="请输入代理商名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="候选人姓名" prop="cdName">
        <el-input v-model="queryParams.cdName" placeholder="请输入候选人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idNumber">
        <el-input v-model="queryParams.idNumber" placeholder="请输入身份证号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="套餐类型">
        <el-select v-model="queryParams.packageType" placeholder="全部">
          <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="背调编号" prop="number">
        <el-input v-model="queryParams.number" placeholder="请输入背调编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="操作人" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="全部">
          <el-option
            v-for="i in peoplelist"
            :key="i.userId"
            :label="i.userName"
            :value="i.userId">
            <!-- value 的值userName   or    userId -->
          </el-option>
        </el-select>
        <!-- <el-input v-model="queryParams.userName" placeholder="请输入操作人" clearable @keyup.enter.native="handleQuery" /> -->
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="全部">
          <el-option v-for="item in optionsStatus" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报告结果" prop="orderOutcome">
        <el-select v-model="queryParams.orderOutcome" placeholder="全部">
          <el-option v-for="item in optionsOutcome" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker style="width: 210px;" v-model="daterangeCreateTime" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
    </el-form>
    <div style="float: right; transform: translateY(-30px);">
      <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">查询</el-button>
      <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
    </div>
    <el-divider></el-divider>
    <div style="display: flex; justify-content: flex-end;">
      <el-button type="primary" icon="el-icon-download" size="small" @click="handleExport"
        v-hasPermi="['order:order:export']">导出</el-button>
    </div>
    <el-table v-if="this.Idtype == 0 || this.Idtype == 1" ref="multipleTable" :row-key="getRowKey" v-loading="loading"
      :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" :reserve-selection="true">
      </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="商家名称" align="center" prop="storeName" width="80" />
      <el-table-column label="所属代理商名称" align="center" prop="agentName" width="120" />
      <el-table-column label="候选人" align="center" prop="cdName" width="80" />
      <el-table-column label="身份证号" align="center" prop="idNumber" width="180" />
      <el-table-column label="套餐类型" align="center" prop="packageType" width="100">
        <template slot-scope="scope">
          <p v-if="scope.row.packageType === '0'">线上</p>
          <p v-if="scope.row.packageType === '1'">线下</p>
        </template>
      </el-table-column>
      <el-table-column label="背调编号" align="center" prop="number" width="180" />
      <el-table-column label="价格" align="center" prop="amount" width="80" />
      <el-table-column label="操作人" align="center" prop="createBy" width="80" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="状态" prop="orderStatus" width="100">
        <template slot-scope="scope">
          <p v-if="scope.row.orderStatus === 0">发起背调</p>
          <p v-if="scope.row.orderStatus === 1">待授权</p>
          <p v-if="scope.row.orderStatus === 2">背调中</p>
          <p v-if="scope.row.orderStatus === 3">已出报告</p>
        </template>
      </el-table-column>
      <el-table-column label="结果" prop="orderOutcome" width="100">
        <template slot-scope="scope">
          <p v-if="scope.row.orderOutcome == '1'">待出具</p>
          <p v-if="scope.row.orderOutcome == '2'">无风险</p>
          <p v-if="scope.row.orderOutcome == '3'" class="blue-text">低风险</p>
          <p v-if="scope.row.orderOutcome == '4'" class="yellow-text">中风险</p>
          <p v-if="scope.row.orderOutcome == '5'" class="red-text">高风险</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['order:order:edit']">查看报告</el-button>
          <el-button size="mini" type="text" icon="el-icon-download" @click="handleDown(scope.row)"
            v-hasPermi="['order:order:remove']">下载报告</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleProgress(scope.row)"
            v-hasPermi="['order:order:remove']">查看进度</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table v-if="this.Idtype == 2" ref="multipleTable" :row-key="getRowKey" v-loading="loading" :data="orderList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" :reserve-selection="true">
      </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="候选人" align="center" prop="cdName" width="80" />
      <el-table-column label="身份证号" align="center" prop="idNumber" width="180" />
      <el-table-column label="套餐类型" align="center" prop="packageType">
        <template slot-scope="scope">
          <p v-if="scope.row.packageType === '0'">线上</p>
          <p v-if="scope.row.packageType === '1'">线下</p>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          <p v-if="scope.row.payStatus === '0'">未支付</p>
          <p v-if="scope.row.payStatus === '1'">已支付</p>
        </template>
      </el-table-column>
      <el-table-column label="背调编号" align="center" prop="number" width="180" />
      <el-table-column label="价格" align="center" prop="amount" width="60" />
      <el-table-column label="操作人" align="center" prop="createBy" width="70" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="状态" prop="orderStatus" width="100">
        <template slot-scope="scope">
          <p v-if="scope.row.orderStatus === 0">发起背调</p>
          <p v-if="scope.row.orderStatus === 1">待授权</p>
          <p v-if="scope.row.orderStatus === 2">背调中</p>
          <p v-if="scope.row.orderStatus === 3">已出报告</p>
        </template>
      </el-table-column>
      <el-table-column label="结果" prop="orderOutcome" width="100">
        <template slot-scope="scope">
          <p v-if="scope.row.orderOutcome == '1'">待出具</p>
          <p v-if="scope.row.orderOutcome == '2'">无风险</p>
          <p v-if="scope.row.orderOutcome == '3'" class="blue-text">低风险</p>
          <p v-if="scope.row.orderOutcome == '4'" class="yellow-text">中风险</p>
          <p v-if="scope.row.orderOutcome == '5'" class="red-text">高风险</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['order:order:edit']">查看报告</el-button>
          <el-button size="mini" type="text" icon="el-icon-download" @click="handleDown(scope.row)"
            v-hasPermi="['order:order:remove']">下载报告</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleProgress(scope.row)"
            v-hasPermi="['order:order:remove']">查看进度</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, downloadReport } from "@/api/order/order";
import { getType } from "@/api/dataStatistics/dataStatistics.js";
import { listMyUser} from "@/api/system/user";
export default {
  name: "Order",
  data() {
    return {
      peoplelist: [],
      // 时间范围
      daterangeCreateTime: [],
      optionsStatus: [{
        value: 0,
        label: '发起背调'
      }, {
        value: 1,
        label: '待授权'
      }, {
        value: 2,
        label: '背调中'
      }, {
        value: 3,
        label: '已出报告'
      }, {
        value: 4,
        label: '异常'
      }, {
        value: 5,
        label: '挂起'
      }],
      optionsOutcome: [{
        value: 1,
        label: '待出具'
      }, {
        value: 2,
        label: '无风险'
      }, {
        value: 3,
        label: '低风险'
      }, {
        value: 4,
        label: '中风险'
      }, {
        value: 5,
        label: '高风险'
      }],
      typeList: [{
        value: '0',
        label: '线上'
      }, {
        value: '1',
        label: '线下'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 背调订单表格数据
      orderList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agentName: null,
        cdName: null,
        idNumber: null,
        packageType: null,
        number: null,
        storeName: null,
        orderStatus: null,
        orderOutcome: null,
        // setmealId: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      Idtype: '',
      multipleSelection: []
    };
  },
  created() {
    this.getList();
    this.getPeoplelist();
    getType().then(res => {
      this.Idtype = res.data.type
    });
  },
  methods: {
    // 拿到操作人列表
    getPeoplelist() {
      listMyUser().then(res => {
        this.peoplelist = res.data;
      })
    },
    // 获取行的唯一键值
    getRowKey(row) {
      return row.id; // 假设每行数据都有一个唯一的 'id' 字段
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    /** 查询背调订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      console.log(this.multipleSelection);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        number: null,
        cdName: null,
        idNumber: null,
        amount: null,
        authorizationType: null,
        payStatus: null,
        orderStatus: null,
        orderOutcome: null,
        storeName: null,
        agentName: null,
        packageType: null,
        setmealId: null,
        parentId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 查看报告
    handleView() {

    },
    /** 下载按钮操作 */
    handleDown(row) {
      downloadReport({ orderId: row.id }).then((res) => {
        let url = res.data;
        const link = document.createElement('a');
        link.href = url;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      }).catch(err => {
        console.error('下载失败:', err);
      });
    },

    // 查看进度
    handleProgress(row) {
      if (row.packageType == '0') {
        this.$router.replace({ path: '/reportDetails/ReportListProgressup', query: { info: row.id } });
      } else if (row.packageType == '1') {
        this.$router.replace({ path: '/reportDetails/ReportListProgressdown', query: { info: row.id } });
      }
    },
  }
};
</script>
